package com.glsc.ngateway.filetransfer.aspect;


import com.glsc.ngateway.common.api.filetransfer.resp.FileTransferResponse;
import com.glsc.ngateway.filetransfer.exception.FileTransferException;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestController
@RestControllerAdvice
@Slf4j
public class WebLogExceptionControllerAdvice {
    private static final Logger logger = LoggerFactory.getLogger(WebLogExceptionControllerAdvice.class);

    @ExceptionHandler(value = IllegalArgumentException.class)
    public FileTransferResponse myErrorHandler(IllegalArgumentException e) {
        log.error(">>>拦截参数异常>>" + e.getMessage());
        FileTransferResponse result = FileTransferResponse.failError("BAD_REQUEST=" + e.getMessage(), e.getLocalizedMessage());
        return result;
    }

    @ExceptionHandler(value = FileTransferException.class)
    public FileTransferResponse amExceptionHandler(FileTransferException e) {
        logger.error("操作异常：", e);
        return new FileTransferResponse(e.getStatus(), e.getMsg(), e.getThrowable());
    }

    @ExceptionHandler(value = DataAccessException.class)
    public FileTransferResponse dataAccessException(DataAccessException e) {
        logger.error(e.toString(), e);
        return FileTransferResponse.failError("数据录入有误! " + e.getRootCause().getMessage(), e.getLocalizedMessage());
    }
}
